package cn.daniel.goods.controller;


import cn.daniel.commonutils.Response;
import cn.daniel.goods.client.VodClient;
import cn.daniel.goods.entity.GoodsVideo;
import cn.daniel.goods.service.GoodsVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Daniel_li
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/service/goods/video")
public class GoodsVideoController {
    @Autowired
    private GoodsVideoService goodsVideoService;
    @Autowired
    private VodClient vodClient;

    /**
     * 添加一个物品视频记录
     * @param goodsVideo
     * @return
     */
    @PostMapping("/addGoodsVideo")
    public Response addGoodsVideo(@RequestBody GoodsVideo goodsVideo){

        boolean save = goodsVideoService.save(goodsVideo);
        return save?Response.ok():Response.error();
    }

    /**
     * 据物品id和视频id删除对应的视频记录（一个）
     * @param goodsId
     * @param videoId
     * @return
     */
    @DeleteMapping("/removeGoodsVideoById/{goodsId}/{videoId}")
    public Response removeGoodsVideoById(@PathVariable String goodsId,@PathVariable String videoId ){
        //先删除数据库中物品视频记录
        QueryWrapper<GoodsVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        wrapper.eq("video_source_id",videoId);
        boolean remove = goodsVideoService.remove(wrapper);

        if (remove){
            //TODO  远程调用vod服务 删除阿里云里面的视频

            return Response.ok();
        }else {

            return Response.error();
        }

    }

    /**
     * 根据goodsId 查看视频信息
     * @param goodsId
     * @return
     */
    @GetMapping("/getGoodsVideoById/{goodsId}")
    public Response getGoodsVideoById(@PathVariable String goodsId){
        GoodsVideo goodsVideo = goodsVideoService.getById(goodsId);
        return Response.ok().data("goodsVideo",goodsVideo);
    }



}

